package org.hackystat.devcathlon.ui.page.matches;

import static org.junit.Assert.assertNotNull;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.tester.DummyPanelPage;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.TestPanelSource;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.commentary.Comment;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.event.EventManager;
import org.hackystat.devcathlon.engine.event.EventScore;
import org.hackystat.devcathlon.engine.event.impl.BuildBeforeCommit;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.engine.user.UserManager;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests MatchDetailPage.
 * 
 * @author John Zhou
 */
public class TestMatchDetailPage extends DevcathlonTestHelper {
  /** The actual build and commit interval. */
  private int buildCommitInterval = 10;
  /** The actual commit streak threshold. */
  private int streakThreshold = 10;
  /** The actual commit streak threshold. */
  private int bonusWeight = 10;

  private String teamName1 = "TestTeam1";
  private String teamOwner1 = "TeamOwner1";
  private String matchName1 = "TestMatch1";
  private String member1a = "Member1a";
  private String member1b = "Member1b";
  private String teamName2 = "TestTeam2";
  private String teamOwner2 = "TeamOwner2";
  private String member2a = "Member2a";
  private String member2b = "Member2b";
  /** Instance of an team. */
  Team team1;
  /** Instance of an team. */
  Team team2;
  /** Instance of an event. */
  private Event event;
  /** Instance of an event. */
  private Comment comment;
  /** Sensor base host URL. */
  private String sensorBaseHost = "http://localhost:9976/sensorbase/";
  /** Daily project data host URL. */
  private String dailyProjectDataHost = "http://localhost:9977/dailyprojectdata/";
  /** Instance of DevcathlonApplication. */
  private DevcathlonApplication devcathlonApplication;
  /** Instance of WicketTester. */
  private WicketTester tester;

  /**
   * Sets up a Devcathlon Configuration for testing.
   * 
   * @throws Exception an error
   */
  @Before
  public void setUp() throws Exception {
    String eventName = BuildBeforeCommit.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 10);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 60);
    configManager.addProperty(eventName, BuildBeforeCommit.buildCommitKey, String
        .valueOf(buildCommitInterval));
    configManager.addProperty(eventName, BuildBeforeCommit.upperThresholdKey, String
        .valueOf(streakThreshold));
    configManager.addProperty(eventName, BuildBeforeCommit.meetThresholdKey, String
        .valueOf(bonusWeight));

    // Create the teams.
    team1 = makeTeam(teamName1, teamOwner1, member1a, member1b);
    team2 = makeTeam(teamName2, teamOwner2, member2a, member2b);
    // Create the projects.
    Project project1 = getProject(teamOwner1, teamName1);
    Project project2 = getProject(teamOwner2, teamName2);
    // Create the event.
    event = new BuildBeforeCommit();
    // Create and add to events table.
//    event = EventManager.getInstance().addEvent(event);
    // Create a match containing 2 teams, 2 projects, and this event.
    Match match = addTwoTeamMatch(matchName1, team1, team2, project1, project2, event);
    // Compute Score
    EventScore score = event.computeScore(match, team1, Tstamp.makeTimestamp());
    match.addScore(score, event);
    comment = new Comment(UserManager.getInstance().getUser("TeamOwner1@hackystat.org"),
        Tstamp.makeTimestamp(), "Good Job");
    match.addComment(comment);
    devcathlonApplication = new DevcathlonApplication(dailyProjectDataHost, sensorBaseHost);
    tester = new WicketTester(devcathlonApplication);
  }

  /**
   * Test Match Detail Page.
   * 
   * @throws Exception If there is an error during testing.
   */
  @Test
  public void testPage() throws Exception {
    tester.setupRequestAndResponse();
    tester.startPage(devcathlonApplication.getHomePage());
    tester.assertRenderedPage(devcathlonApplication.getHomePage());

    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", "admin@hackystat.org");
    formTester.setValue("password", "admin@hackystat.org");
    formTester.submit();

    tester.clickLink("MatchesLink");
    tester.assertRenderedPage(MatchesPage.class);
    tester.clickLink("MatchesPanel:ItemList:1:MatchDetailsLink");
    tester.assertRenderedPage(MatchDetailPage.class);
    assertNotNull("wicket ID found.", tester.getTagByWicketId("MatchDetailPanel"));
    TestPanelSource testPanelSource = new TestPanelSource() {
      private static final long serialVersionUID = 1L;
      public Panel getTestPanel(String wicketId) {
        return new MatchDetailPanel(wicketId, matchName1);
      }
    };
    tester.startPanel(testPanelSource);
    tester.assertLabel("panel:MatchLabel", "TestMatch1");
    tester.assertLabel("panel:ScoreList:1:EventLabel", event.getName());
    tester.assertLabel("panel:ScoreList:1:TeamLink:TeamLabel", team1.getName());
    tester.assertLabel("panel:CommentList:1:CommentLabel", comment.getComment());
    
    tester.clickLink("panel:OrderByTeams:orderByLink");
    tester.assertRenderedPage(DummyPanelPage.class);
    tester.clickLink("panel:OrderByEvents:orderByLink");
    tester.assertRenderedPage(DummyPanelPage.class);
    tester.clickLink("panel:OrderByPoints:orderByLink");
    tester.assertRenderedPage(DummyPanelPage.class);
    tester.clickLink("panel:OrderByDates:orderByLink");
    tester.assertRenderedPage(DummyPanelPage.class);
    
//    FormTester commentTester = tester.newFormTester("panel:CommentForm");
//    commentTester.setValue("Content", "Test");
//    commentTester.submit();
//    tester.assertLabel("panel:CommentList:3:CommentLabel", "Test");
  }
}
